import { Component, OnInit } from '@angular/core';
import {Course} from '../student';
import {HttpService} from '../http.service';
import {MessageService} from '../message.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-course-select',
  templateUrl: './course-select.component.html',
  styleUrls: ['./course-select.component.css']
})
export class CourseSelectComponent implements OnInit {
  coursesshow1: Course[] = [];
  searchValue = '';
  visible = false;
  visible2 = false;
  searchValue2 = '';
  courses: Course[] = [];
  pageIndex = 1;
  pageSize = 10;
  coursesshow: Course[] = [];
  loading = true;
  total = 10000;
  constructor(private httpService: HttpService,
              public router: Router,
              private messageService: MessageService) { }
  // search(stuid: string) {
  //   this.httpService.course_select_stuid(stuid).subscribe(
  //     res => {
  //       if (res.status === 1) {
  //         this.messageService.add('ok');
  //         const good = this.courses.find(obj => obj.stuid === stuid);
  //         this.courseid.push(good);
  //       }
  //     }
  //   );
  // }
  reset(): void {
    this.coursesshow = this.coursesshow1;
  }
  search() {
    this.visible = false;
    this.coursesshow = this.coursesshow.filter((item: Course) => item.courseid.indexOf(this.searchValue) !== -1);
  }
  search3() {
    this.visible2 = false;
    this.coursesshow = this.coursesshow.filter((item: Course) => String(item.stuid).indexOf(this.searchValue2) !== -1);
  }
  getStudents(): void {
    this.httpService.course_select().subscribe(
      res => {
        res.forEach(item => {
          this.courses = res;
          this.courses.push({
            courseid: item.courseid,
            score: item.score,
            stuid: item.stuid
          });
        });
        // for ()
        this.coursesshow = this.courses;
        this.coursesshow1 = this.courses;
        this.loading = false;
      }
    );
  }
  // searchData(): void {
  //   this.coursesshow = this.courses.slice((this.pageIndex - 1) * this.pageSize, (this.pageIndex) * this.pageSize);
  // }
  //
  // click() {
  //   console.log(this.courses);
  //   this.searchData();
  // }
  ngOnInit(): void {
    this.getStudents();
  }
}
